import Vue from 'vue'
import Router from 'vue-router'
import fLogin from '../components/front/fLogin'
import fRegister from '../components/front/fRegister'
import fMain from '../components/front/fMain'
import fQuestion from '../components/front/fQuestion'
import fShare from '../components/front/fShare'
import ChatRoom from '../components/ChatRoomTest/ChatRoom'
import fRoom from '../components/front/fRoom'
import fVideo from '../components/front/fVideo'
import fHome from '../components/front/fHome'
import fPlay from '../components/front/fPlay'
import badminLogin from '../components/back/badminLogin'
import bContact from '../components/back/bContact'
import bUsers from '../components/back/bUsers'
Vue.use(Router)

// 在路由跳转的时候同一个路由多次添加是不被允许的
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


let router = new Router({
  routes: [
    {path: '/', redirect: '/flogin'},
    {path: '/badminLogin', name: 'badminLogin', component: badminLogin},
    {path: '/bContact', name: 'bContact', component: bContact},
    {path: '/bUsers', name: 'bUsers', component: bUsers},
    {path: '/flogin', name: 'flogin', component: fLogin},
    {path: '/fregister', name: 'fregister', component: fRegister},
    {
      path: '/fmain',
      name: 'fmain',
      meta: {requireAuth: true},
      component: fMain,
      children: [
        { path: 'fquestion', component: fQuestion ,name: 'fquestion'},
        { path: 'fshare', component: fShare, name: 'fshare' },
        { path: 'fRoom', component: fRoom ,name: 'fRoom'},
        { path: 'fVideo', component: fVideo ,name: 'fVideo'},
        { path: 'fHome', component: fHome ,name: 'fHome'},
        { path: 'fPlay', component: fPlay ,name: 'fPlay'},
      ]
    },
    {path: '/chat', name: 'chat', component: ChatRoom,},

  ],linkActiveClass: '' // 覆盖默认的路由高亮的类，默认的类叫做 router-link-active
})

export default router

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (sessionStorage.getItem('isLogin')) { // 判断本地是否存在access_token
      next()
    } else {
      // 未登录,跳转到登陆页面，并且带上 将要去的地址，方便登陆后跳转。
      next({
        path: '/',
        // query:{ referrer: to.fullPath}
      })
    }
  } else {
    next()
  }
})
